#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 21 18:02:15 2021

@author: ljl
"""

import tensorflow as tf
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
import glob
from skimage import io, transform
import cv2 as cv
import copy

from imageReady import generateDataSet,_show_time, slipImgChannel, showLoss

#load  model
save_path = './model'
# 载入数据集
path = "/home/ljl/Desktop/newImagesData/merge"

row = 28
col = 28
c = 1
img_type = '.png'
class_num = 6
# 读取数据
imgs, labels = generateDataSet("/home/ljl/Desktop/newImagesData/merge", row, col)
if imgs.shape[3] != 2:
    print("imgs channel error: ", imgs.shape)
    sys.exit(0)

left, right = slipImgChannel(imgs)
# 加载模型
with tf.Session(graph=tf.Graph())as sess:

    # tf.saved_model.loader.load(sess,[tf.saved_model.tag_constants.TRAINING],save_path)
    sess.run(tf.global_variables_initializer())  # 加了这句话就会导致图初始化

    # 加载模型
    saver = tf.train.import_meta_graph(save_path + '/model.ckpt.meta')  # 先加载meta文件，具体到文件名
    saver.restore(sess, tf.train.latest_checkpoint(save_path))  # 加载检查点文件checkpoint，具体到文件夹即可
    graph = tf.get_default_graph()  # 绘制tensorflow图
    output = sess.graph.get_tensor_by_name('conv5:0')

    aaa = sess.run(output,feed_dict={left:left[0], right:right[0] })
    print(aaa)

    if 0:
        aaaa = 0

    else:
 
        print('>>>>>>>>>>>>>>>\t end \t<<<<<<<<<<<<<')
        '''
        for i in range(imgs.shape([0]):
            prediction = sess.run(
                output, feed_dict={input_x: test_img[i:i+1], input_y: test_label[i:i+1], keep_prob: 1.0})

            pre_idx = np.argmax(prediction[0])

            print('Training prediction=', prediction)
            print(' -> output class is: ', pre_idx + 1, "\n",
                  '-> prediction   is: ', prediction[0][pre_idx])
            print('>>>>>>>>>>>>>>>\t end \t<<<<<<<<<<<<<')
            
            #0  4  5  2  3  1  
            if pre_idx+1 != 3 and prediction[0][pre_idx] > 0.97:    #0  1  2  3
                print("*************delete")
                cv.imshow('aa', test_show[i])
                cv.waitKey(0)
                cv.destroyAllWindows()
                continue
            else:       #4
                cv.imwrite(save_process_roi + str(i) + ".png",test_show[i])
                
                cv.imshow('aa', test_show[i])
                cv.waitKey(0)
                cv.destroyAllWindows()
        '''

    # 0  4   2   3   1